package org.teremail.tx;

import java.sql.SQLException;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.xa.XAResource;

import org.teremail.util.DB;

public class SimpleTransaction implements Transaction {
    
    private DelegateConnection cn = null;

    public void commit() throws RollbackException, HeuristicMixedException,
            HeuristicRollbackException, SecurityException,
            IllegalStateException, SystemException {
        if (cn != null) {
            try {
                cn.commit();
            } catch (SQLException e) {
                throw new SystemException(e.getMessage());
            } finally {
                DB.close(cn.getConnection());
                cn = null;
            }
        }
    }

    public boolean delistResource(XAResource arg0, int arg1)
            throws IllegalStateException, SystemException {
        throw new RuntimeException("Not Implemented");
    }

    public boolean enlistResource(XAResource arg0) throws RollbackException,
            IllegalStateException, SystemException {
        throw new RuntimeException("Not Implemented");
    }

    public int getStatus() throws SystemException {
        // TODO Auto-generated method stub
        return 0;
    }

    public void registerSynchronization(Synchronization arg0)
            throws RollbackException, IllegalStateException, SystemException {
        // TODO Auto-generated method stub
        
    }

    public void rollback() throws IllegalStateException, SystemException {
        if (cn != null) {
            try {
                cn.rollback();
            } catch (SQLException e) {
                throw new SystemException(e.getMessage());
            } finally {
                DB.close(cn.getConnection());
                cn = null;
            }
        }
    }

    public void setRollbackOnly() throws IllegalStateException, SystemException {
        // TODO Auto-generated method stub
    }
    
    public DelegateConnection getConnection() {
        return cn;
    }
    
    public void setConnection(DelegateConnection cn) {
        this.cn = cn;
    }

}
